﻿using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;

namespace TicketM.Configuration
{
    public partial class Routing : System.Web.UI.Page
    {
        private string RoutingConfigPath;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["sProject"].ToString() == "" || Session["sProject"] == null)
            {
                Session.Add("refPage", HttpContext.Current.Request.Url);
                Response.Redirect("ProjectSelection.aspx");
            }
            lblProject.Text = Session["sProject"].ToString();
            if (!Page.IsPostBack)
            {
                TicketM.Models.Tickets areas = new Models.Tickets();
                try
                {
                    areas.GetRoutingConditions(areas.GetAreasStructure(Session["sProject"].ToString()), Session["sProject"].ToString());
                }
                catch (Exception ex)
                {
                    Logger.CreateLogEntry("!!ERROR in Routing.aspx.cs while getting routing conditions: " + ex.Message);
                    throw;
                }
            }
            if (Process.GetCurrentProcess().ProcessName.ToLower().Contains("iisexpress"))
            {
                RoutingConfigPath = "/App_Data/AreasStruct.xml";
            }
            else
            {
                RoutingConfigPath = "/TicketM/App_Data/AreasStruct.xml";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow selectedRow = GridView1.SelectedRow;
            AreaPath.Text = selectedRow.Cells[1].Text;
            AssignedTo.SelectedItem.Text = selectedRow.Cells[2].Text;
            StoreButton.Enabled = true;
        }

        protected void StoreButton_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath(RoutingConfigPath));
            XmlNode root = doc.DocumentElement;
            XmlNode node = root.SelectSingleNode("SubArea[@Name='" + AreaPath.Text + "']");
            try
            {
                if (node.HasChildNodes)
                {
                    node.FirstChild.InnerText = AssignedTo.SelectedItem.Text;
                    doc.Save(HttpContext.Current.Server.MapPath(RoutingConfigPath));
                    StoreButton.Enabled = false;
                }
                else
                {
                    XmlNode userNode = doc.CreateNode(XmlNodeType.Element, "Assignee", null);
                    userNode.InnerText = AssignedTo.SelectedItem.Text;
                    node.AppendChild(userNode);
                    doc.Save(HttpContext.Current.Server.MapPath(RoutingConfigPath));
                    StoreButton.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                Logger.CreateLogEntry("!!ERROR in Routing.aspx.cs while storing the routing data. : " + ex.Message);
                Response.Redirect("../CommonErrorPage.aspx");
            }
            GridView1.DataBind();
            AssignedTo.DataBind();
            UpdateButton.Enabled = true;
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            Models.Tickets Ticketing = new Models.Tickets();
            if (Ticketing.UpdateRouting(Session["sProject"].ToString()))
            { 
                UpdateButton.Enabled = false;
            }
            
        }
    }
}